Sky = class()
Sky.M = 20
Sky.N = 20

function Sky:init(radius)
    self.radius = radius
    self.sky = mesh()
    local vertices = {}
    local colors = {}
    local z = math.pi / Sky.M
    local xy = 2 * math.pi / Sky.N
    local az = 0
    local axy = 0
    local i,j = 0,0
    local normal = color(156, 196, 223, 255)
    -- if TimeUI():IsInDay(2,15) == true then
        -- normal = color(210, 144, 139, 255)
    -- end
    local p = 1
    local sin,cos = math.sin,math.cos
    for i = 0,Sky.M - 1 do
        az = i * z
        for j = 0,Sky.N - 1 do
            axy = j * xy
            
            vertices[p] = vec3(radius * sin(az + z) * cos(axy + xy),radius * sin(az + z) * sin(axy + xy),radius * cos(az + z))
            vertices[p + 1] = vec3(radius * sin(az + z) * cos(axy),radius * sin(az + z) * sin(axy),radius * cos(az + z))
            vertices[p + 2] = vec3(radius * sin(az) * cos(axy),radius * sin(az) * sin(axy),radius * cos(az))
            vertices[p + 3] = vertices[p]
            vertices[p + 4] = vertices[p + 2]
            vertices[p + 5] = vec3(radius * sin(az) * cos(axy + xy),radius * sin(az) * sin(axy + xy),radius * cos(az))
            
            for k = 0,5 do
                local pk = p + k
                local z = vertices[pk].z
                if z < -20000 then
                    colors[pk] = color(0, 114, 255, 255)
                elseif z < -8000 then
                    colors[pk] = color(78, 155, 239, 255)
                elseif z < -4000 then
                    colors[pk] = color(156, 196, 223, 255)
                elseif z < 400 then
                    colors[pk] = color(235, 235, 235, 255)
                elseif z < 2000 then
                    colors[pk] = color(215, 225, 232, 255)
                else
                    colors[pk] = normal
                end
            end
            
            p = p + 6
        end
    end
    
    self.sky.vertices = vertices
    self.sky.colors = colors
    self.sky.shader = shader("Project:SkyShader")
    -- self.sky:setColors(10,10,10,255)
    
    self.sun = mesh()
    self.sun.texture = "Project:sun"
    self.sun.vertices = {
    vec3(0,-100,-100),
    vec3(0,-100,100),
    vec3(0,100,100),
    vec3(0,100,100),
    vec3(0,100,-100),
    vec3(0,-100,-100)
    }
    self.sun.texCoords = {
    vec2(-0,-0),
    vec2(0,1),
    vec2(1,1),
    vec2(1,1),
    vec2(1,0),
    vec2(0,0)
    }
    self.sun:setColors(255,255,255,255)
    self.sun.shader = shader("Project:TextureShader")
end

function Sky:render()
    pushMatrix()
    rotate(-90,1,0,0)
    self.sky.shader.GlobalColor = world.globalColor
    self.sky:draw()
    
    resetMatrix()
    local rot = -world:getTime() / 40
    rotate(rot,0,0,1)
    translate(-20000,0,0)
    scale(50)
    self.sun:draw()
    popMatrix()
end
